#ifndef __BIT_H__
#define __BIT_H__

#include <stdint.h>

#define BIT_MASK(OFF, LEN) (((1UL << (OFF + LEN)) - 1) & ~((1UL << (OFF)) - 1))

#ifndef __ASSEMBLER__

static inline uint64_t bit_get(uint64_t word, uint64_t off)
{
    return word & (1UL << off);
}

static inline uint64_t bit_set(uint64_t word, uint64_t off)
{
    return word |= (1UL << off);
}

static inline uint64_t bit_clear(uint64_t word, uint64_t off)
{
    return word &= ~(1UL << off);
}

static inline uint64_t bit_extract(uint64_t word, uint64_t off, uint64_t len)
{
    return (word >> off) & BIT_MASK(0, len);
}

static inline uint64_t bit_insert(uint64_t word, uint64_t val, uint64_t off,
                                  uint64_t len)
{
    return (~BIT_MASK(off, len) & word) | ((BIT_MASK(0, len) & val) << off);
}

#endif /* |__ASSEMBLER__ */

#endif /* __BIT_H__ */
